<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="物料编码" prop="materialCode">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警类型" prop="warningType">
        <el-select
          v-model="queryParams.warningType"
          placeholder="请选择预警类型"
          clearable
        >
          <el-option
            v-for="dict in warningTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预警状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择预警状态"
          clearable
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['wms:warning:add']"
        >新增规则</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:warning:edit']"
        >修改规则</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:warning:remove']"
        >删除规则</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-bell"
          @click="handleNotify"
          v-hasPermi="['wms:warning:notify']"
        >发送通知</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-download"
          @click="handleExport"
          v-hasPermi="['wms:warning:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <!-- 表格数据 -->
    <el-table
      v-loading="loading"
      :data="warningList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预警编号" align="center" prop="warningId" />
      <el-table-column label="物料编码" align="center" prop="materialCode" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="预警类型" align="center" prop="warningType">
        <template slot-scope="scope">
          <dict-tag :options="warningTypeOptions" :value="scope.row.warningType"/>
        </template>
      </el-table-column>
      <el-table-column label="预警阈值" align="center" prop="threshold" />
      <el-table-column label="当前库存" align="center" prop="currentQuantity" />
      <el-table-column label="预警状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '1' ? 'danger' : 'success'">
            {{ scope.row.status === '1' ? '预警中' : '正常' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:warning:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['wms:warning:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预警规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="预警类型" prop="warningType">
          <el-select v-model="form.warningType" placeholder="请选择预警类型">
            <el-option
              v-for="dict in warningTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="预警阈值" prop="threshold">
          <el-input-number v-model="form.threshold" :min="0" :step="1" />
        </el-form-item>
        <el-form-item label="通知方式" prop="notifyTypes">
          <el-checkbox-group v-model="form.notifyTypes">
            <el-checkbox label="1">系统消息</el-checkbox>
            <el-checkbox label="2">邮件通知</el-checkbox>
            <el-checkbox label="3">短信通知</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarning, getWarning, addWarning, updateWarning, deleteWarning, notifyWarning } from "@/api/wms";
import { getDicts } from "@/api/system/dict/data";

export default {
  name: "Warning",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 预警表格数据
      warningList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 预警类型数据字典
      warningTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialCode: undefined,
        materialName: undefined,
        warningType: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialCode: [
          { required: true, message: "物料编码不能为空", trigger: "blur" }
        ],
        materialName: [
          { required: true, message: "物料名称不能为空", trigger: "blur" }
        ],
        warningType: [
          { required: true, message: "预警类型不能为空", trigger: "change" }
        ],
        threshold: [
          { required: true, message: "预警阈值不能为空", trigger: "blur" }
        ],
        notifyTypes: [
          { required: true, message: "请至少选择一种通知方式", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts();
  },
  methods: {
    /** 查询预警列表 */
    getList() {
      this.loading = true;
      listWarning(this.queryParams).then(response => {
        this.warningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取数据字典
    getDicts() {
      getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
      getDicts("wms_warning_type").then(response => {
        this.warningTypeOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        warningId: undefined,
        materialCode: undefined,
        materialName: undefined,
        warningType: undefined,
        threshold: undefined,
        notifyTypes: [],
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warningId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预警规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const warningId = row.warningId || this.ids;
      getWarning(warningId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预警规则";
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      const warningId = row.warningId;
      this.$router.push({ path: "/wms/warning/detail", query: { warningId: warningId } });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warningIds = row.warningId || this.ids;
      this.$modal.confirm('是否确认删除预警编号为"' + warningIds + '"的数据项？').then(function() {
        return deleteWarning(warningIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 发送通知按钮操作 */
    handleNotify() {
      if (this.single) {
        this.$modal.msgError("请选择一条记录进行操作");
        return;
      }
      const warningId = this.ids[0];
      notifyWarning(warningId).then(response => {
        this.$modal.msgSuccess("通知发送成功");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warningId != null) {
            updateWarning(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarning(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/warning/export', {
        ...this.queryParams
      }, `warning_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>